﻿/// <reference path="jquery.js" />

/**
 * jquery.temp imsoftgroup copy;
 */

; (function ($) {

    var plugin = {};
    var showpageobj = null;
    var defaults = {
        // GENERAL
        jstemp: 'test',//js模板ID
        url: 'json/notyethonored.json',//json
        bottom:20,//设置到底部的间距
        data: {},//要传递的参数
        load: '<div class="pad-50 center load" ><img src="../images/load1.gif"  /></div>',
        curpage: 1,//当前页码 如果不显示页面则为 null
        showpage: 20,
        datacache:false,
        total:100,//总页数
        idpage: 'page1',//分页ID
        cache: null,
        nulldata: "<div class='null-data'><span>null data<span></div>",
        // 回调
        callback: function (data) { }
    }

    $.fn.imtemp = function (options) {
        if (this.length == 0) return this;
        if (this.length > 1) {
            this.each(function () { $(this).imtemp(options) });
            return this;
        }
        var temp = {};
        var el = this;
        plugin.el = this;
        var scrolltime = false;
        var init = function () {
            // merge user-supplied options with the defaults
           
            temp.settings = $.extend({}, defaults, options);
            
            el.star(temp.settings.curpage);
            var roadn = Math.floor(Math.random() * 1000000000);
            el.addClass("s" + roadn)
            //console.log(temp.settings.url + "?" + temp.settings.data)

            if (temp.settings.curpage != null) {
                //滚动加载console.log(10)
                    $(window).unbind('scroll');
                    $(window).scroll(function () {                      
                        if ($(".s" + roadn).is(":visible")) {
                        	
                            if (scrolltime) {
                                var winh = $(window).height();
                                var bodyh = $(".html-box").height();
                                var tp = $(window).scrollTop();                               
                                //设置缓存位置
                                if (!!temp.settings.cache && !!localStorage.getItem(temp.settings.cache)) {
                                    var sn2 = JSON.parse(localStorage.getItem(temp.settings.cache))
                                    sn2.scroll = tp;
                                    localStorage.setItem(temp.settings.cache, JSON.stringify(sn2))
                                }

                                if (tp + winh > bodyh - temp.settings.bottom) {                                  
                                    scrolltime = false;
                                    temp.settings.curpage += 1;
                                    if (temp.settings.total == 0) return;
                                    if (temp.settings.curpage <= temp.settings.total) {
                                        el.star(temp.settings.curpage);
                                    } else {
                                        $(window).unbind('scroll');
                                    }

                                }
                            }
                        } else {
                            $(window).unbind('scroll');
                        }
                       
                    })
                }            
        }
       
        el.star = function (num) {           
            //console.log(temp.settings.url + "?" + temp.settings.data)
            var num = num
            var datas = temp.settings.data
            if (temp.settings.curpage != null)
            {
                datas['page'] = num;
                datas['pagesize'] = temp.settings.showpage;
            }
			console.log(datas)
            el.append(temp.settings.load);
            //设置缓存
            if (!!temp.settings.datacache) {
            	 el.find(".load").remove();
                 el.append(temp.settings.datacache);
                 temp.settings.total = 10000;
                 scrolltime = true;
                 console.log({result:{page:obj_huancuan.page}})
                 temp.settings.callback({result:{page:obj_huancuan.page}});
                 temp.settings.datacache=false;
                 setTimeout(function(){
	       	 		$(window).scrollTop(obj_huancuan.offset);
	       	 	},0)
            } else {
                //不设置缓存
                global_ajax(temp.settings.url, datas, function (msg) {
                    var str = msg;
                    el.find(".load").remove();
                    var html = template(temp.settings.jstemp, str.result);
                    el.append(html);
                    if (num == 1) {
                        if (!str.result.list[0]) {
                            //el.html(temp.settings.nulldata)
                        }
                    }
                    temp.settings.total = str.result.total;
                    scrolltime = true;
                    temp.settings.callback(msg);
                })
            }
        }

        

        init();

        // returns 当前对象
        return this;
    }





})(jQuery);
